<?php
//+-----------------------------------------------------------
//| 人生是荒芜的旅行，冷暖自知，苦乐在心
//+-----------------------------------------------------------
//| Author:Janmas <janmas@126.com>
//+-----------------------------------------------------------
//| 
//+-----------------------------------------------------------


namespace app\common\plugins;


use app\admin\exception\LogicException;
use app\common\event\ConfigEvent;
use app\common\traits\Controller;
use app\common\traits\Jump;
use think\App;
use think\facade\View;

/**
 * 插件模块控制器基类
 * Class PluginController
 * @package app\common\plugins
 */
abstract class PluginController
{
    use Jump;
    use Controller;
    /**
     *
     * @var null
     */
    protected $view = null;

    protected $app = null;

    protected $request = null;

    protected $info = null;

    protected $module = '';

    public static $instance;

    /**
     * 插件标识
     * @var string
     */
    protected $name = '';

    /**
     * 插件地址
     * @var string
     */
    protected $pluginPath = '';

    protected $service = null;

    public function __construct(App $app, $module)
    {
        $this->app = $app;
        $this->request = $this->app->request;
        $this->name = $this->getName();
        //TODO:待增加权限判断
        $this->pluginPath = $this->getPluginPath() . $this->name . DIRECTORY_SEPARATOR;
        $this->view = clone View::engine('Think');
        $this->module = $module;

        if (!is_dir($this->pluginPath . $module . DIRECTORY_SEPARATOR . 'view')) {
            $view_path = $this->pluginPath . 'view' . DIRECTORY_SEPARATOR;
        } else {
            $view_path = $this->pluginPath . $this->module . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR;
        }

        $this->view->config([
            'view_path' => $view_path,
            'cache_path' => app()->getRuntimePath() . 'plugins' . DIRECTORY_SEPARATOR . $this->name . DIRECTORY_SEPARATOR,
            'tpl_replace_string' => [
                '__STATIC__' => '/static/',
                '__COMMON__' => '/static/common/',
                '__EXTRA__' => '/static/extra/',
                '__BOOT__' => '/static/extra/bootstrap/',
                '__LAYUI__' => '/static/extra/layui/',
                '__JQUERY__' => '/static/common/js/jquery-3.5.1/',
                '__ADMIN__' => '/static/admin/',
                '__PLUGIN__' => '/plugins/' . $this->name . '/static',
            ],
            'layout_on' => true,
            'layout_name' => app()->getRootPath() . 'app/admin/view/layout.html'
        ]);
        event(new ConfigEvent());//加载全局配置
        // 控制器初始化
        $this->initialize();
        $this->initService();
    }

    public function initialize()
    {
    }

    /**
     * 获取插件信息
     * @return mixed
     */
    final public function getInfo()
    {
        $jsonFile = $this->getPluginPath() . $this->getFlag() . DIRECTORY_SEPARATOR . 'info.json';
        if (!is_file($jsonFile)) {
            throw new LogicException(sprintf('插件%s的info.json不存在', $this->getFlag()));
        }
        return json_decode(file_get_contents($jsonFile), true);
    }

    final function fetch($template = '', $vars = [])
    {
        if ($this->request->controller() != 'index') {
            $this->view->layout(false);
        }

        return $this->view->fetch($template, $vars);
    }

    final function assign($name = '', $vars = [])
    {
        $this->view->assign($name, $vars);
        return $this;
    }


    protected function display(string $content, array $var = [])
    {
        return $this->view->display($content, $var);
    }

    /**
     * 模板引擎
     * @param $engine
     * @return $this
     */
    protected function engine($engine)
    {
        $this->view->engine($engine);

        return $this;
    }

    /**
     * 获取插件路径
     * @return string
     */
    final protected function getPluginPath()
    {
        return app()->getRootPath() . 'plugins' . DIRECTORY_SEPARATOR;
    }

    /**
     * 获取插件标识
     * @return mixed|string
     */
    final function getName()
    {
        $class = get_class($this);
        list(, , $name,) = explode('\\', $class);

        $this->request->plugin = $name;

        return $name;
    }

    final protected function initService(){
        $class = '\\app\\plugins\\' . $this->name . '\\' . $this->module . '\\service\\'.$this->request->controller();
        if($this->module == 'index' && !class_exists($class)){
            $class = str_replace($this->module.'\\','',$class);
        }
        if(class_exists($class)){
            $this->service = new $class($this->request);
        }
    }

    public function __call($name, $arg)
    {
        if (method_exists($this, $name)) {
            return $this->$name($arg);
        }
    }


}